package com.gimi.cloud.bbp.client.controller;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gimi.cloud.bbp.client.service.UserService;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.enums.ServiceNameConstants;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.RandomUtil;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.commonService.sms.SmsPortal;
import com.gimi.cloud.bbp.entity.user.User;

/**
 * create by xiankejin on 2017-10-13
 */
@RequestMapping("/user")
@RestController
public class UserController{
    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    private final UserService userService;

    // 上传OSS成功FLAG
    private final static int SUCCESSFLAG = 1;
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 采集运行商数据 提交短信验证码
     * @param paramMap
     * @param response
     * @throws MsgException
     */
    @RequestMapping(value = "/getCarrierDataSubmitNoteCode")
    public void getCarrierDataSubmitNoteCode(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) throws MsgException {
        Response resp = new Response();
        String password = StringUtil.isNull(paramMap.get("password"));
        String token = StringUtil.isNull(paramMap.get("rqToken"));
        String website=StringUtil.isNull(paramMap.get("website"));
        String captcha=StringUtil.isNull(paramMap.get("captcha"));//网站短信验证码
        String queryPwd="";//网站查询密码,只有北京移动
        String type="";
        if(null!=captcha && !"".equals(captcha.trim())){
            type="SUBMIT_CAPTCHA";//RESEND_CAPTCHA
        }
        User user = userService.getUserById(Integer.valueOf(paramMap.get("userid").toString()));
        if(user == null) {
            throw new MsgException(ResponseCodeEnum.ERR_USER_INVALID);
        }
        String account=user.getMobilePhone();
        Map<String,Object> params=new HashMap<>();
        params.put("token", token);
        params.put("account", account);
        params.put("password", password);
        params.put("website", website);
        params.put("captcha", captcha);
        params.put("queryPwd", queryPwd);
        params.put("type", type);
        params.put("ip",StringUtil.isNull(paramMap.get("ip")));
//        userService.callInterfaceParseSaveData(response, resp, user, params);

    }

    /**
     * 提交申请表单获取回执信息 运营商
     * @param user
     * @param response
     * @throws MsgException
     */
    @RequestMapping(value = "/applicationFormRetrieveInfo")
    public void applicationFormRetrieveInfo(@RequestBody User user, HttpServletResponse response) throws MsgException {
        Response resp = new Response();

        //获取user数据
        user = userService.getUserById(user.getUserId());
        if(user == null) {
            throw new MsgException(ResponseCodeEnum.ERR_USER_INVALID);
        }

        logger.info("申请开始..............................线蜜罐后聚信立...............................");
        /**在调用聚信立之前先调用蜜罐接口，存储之后放入OSS*/
        //定义变量
        String[] selected_website = {};
        String[] contacts = {};


        Map<String, Object> basic_info = userService.fillBaseInfoToParams(user);
        //params数据封装
        Map<String,Object> params=new HashMap<>();
        params.put("selected_website", selected_website);
        params.put("contacts", contacts);
        params.put("skip_mobile", false);
        params.put("basic_info", basic_info);
        params.put("uid", "");

        //给params集合封装联系人参数
//        List elist = userService.fillContactsToParams(user);
//        if(elist.size()>0){
//            params.put("contacts", elist);
//        }
//        logger.info("params>>>"+ JsonUtil.buildJSON(params));
//        //调用第三方接口
//        Map<String, Object> map = userService.callInterface(user, params);
//        resp.setRes_data(map);
//        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /**
     * 短信分享
     * @param user
     * @throws MsgException
     */
    @RequestMapping(value = "/shareNotes")
    public void shareNotes(@RequestBody User user, HttpServletResponse response) throws MsgException {
        Response resp = new Response();
        Map<String, Object> data = new HashMap<>();
        resp.setRes_data(data);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }
    /**
     * 分享链接给qq和微信
     * @param user
     * @param response
     */
    @RequestMapping(value = "/shareLink")
    public void shareLink(@RequestBody User user, HttpServletResponse response) throws MsgException {
        logger.info("user:" + user);
        Response resp = new Response();

        user = userService.getUserById(user.getUserId());
        if(user == null) {
            throw new MsgException(ResponseCodeEnum.ERR_USER_INVALID);
        }
        Map<String, Object> data = new HashMap<>();
        resp.setRes_data(data);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /**
     * 获取验证码
     * @param user
     * @param response
     */
    @RequestMapping(value = "/getUserRegisterCode")
    public void getUserRegisterCode(@RequestBody User user, HttpServletResponse response) throws MsgException {
        logger.info("user:" + user);
        Response resp = new Response();
        Boolean res = userService.redisFixedTimeSendCode(user.getMobilePhone());
        //判断60秒内不能再次发送
        if(res) {
           throw new MsgException(ResponseCodeEnum.ACCESS_BARRED, ServiceNameConstants.REPEAT_SEND_CODE_INFO);
        }
        // 创建验证码
        String createCode = RandomUtil.getRandomNumString(6);
        logger.info("createCode>>>"+createCode);
        //发送验证码
        String typeNid = SmsPortal.LOGIN_BY_PWDCODE;
        userService.sendCode(user.getMobilePhone(), createCode ,typeNid);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"短信验证码已发送");
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /**
     * 获取初始化密码的验证码
     * @param user
     * @param response
     */
    @RequestMapping(value = "/getInitpasswordPwdCode")
    public void getInitpasswordPwdCode(@RequestBody User user, HttpServletResponse response) throws MsgException {
        logger.info("user:" + user);
        Response resp = new Response();
        Boolean res = userService.redisFixedTimeInitPassword(user.getMobilePhone());
        if(res) {//判断60秒内不能再次发送
            throw new MsgException(ResponseCodeEnum.ACCESS_BARRED, ServiceNameConstants.REPEAT_SEND_CODE_INFO);
        }
        // 创建验证码
        String createCode = RandomUtil.code();
        logger.info("初始化密码的createCode>>>"+createCode);
        //发送验证码
//        userService.sendCode(AppCommons.REGISTER_CODE_MAP, user.getPhone(), createCode);
        userService.sendInitPasswordCode(user.getMobilePhone(), createCode);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"短信验证码已发送");
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /**
     * 获取验证码
     * @param user
     * @param response
     */
    @RequestMapping(value = "/getModifypasswordCode")
    public void getModifypasswordCode(@RequestBody User user, HttpServletResponse response) throws MsgException {
        logger.info("user:" + user);
        Response resp = new Response();
        Boolean res = userService.redisFixedTimeSendModifypasswordCode(user.getMobilePhone());
        if(res) {//判断60秒内不能再次发送
            throw new MsgException(ResponseCodeEnum.ACCESS_BARRED, ServiceNameConstants.REPEAT_SEND_CODE_INFO);
        }
        // 创建验证码
        String createCode = RandomUtil.code();
        logger.info("createCode>>>"+createCode);
        //发送验证码
        String typeNid = SmsPortal.MODIFYPASSWORD_CODE;
        userService.sendCode(user.getMobilePhone(), createCode ,typeNid);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"短信验证码已发送");
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }
}
